/*
 * WarehouseView.java
 */

package warehouse;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.TreeMap;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.*;

/**
 * The application's main frame.
 */
public class WarehouseView extends FrameView {
    
            
        Database db = new Database();
        DecimalFormat twoDForm = new DecimalFormat("##0.00");
        TreeMap<Integer, Double> sorted_map;
        TreeMap<Integer, Integer> sorted_map_int;
        int tableSize = 15;

    public WarehouseView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
      

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
    //                progressBar.setVisible(true);
    //                progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
     //               progressBar.setVisible(false);
     //               progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
      //              progressBar.setVisible(true);
      //              progressBar.setIndeterminate(false);
      //              progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = WarehouseApp.getApplication().getMainFrame();
            aboutBox = new WarehouseAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        WarehouseApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        recordTable = new javax.swing.JTable();
        mostPointingButton = new javax.swing.JButton();
        receiversPlayersButton = new javax.swing.JButton();
        attackersPlayersButton = new javax.swing.JButton();
        defendersPlayersButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        attackTeamButton = new javax.swing.JButton();
        defenceTeamButton = new javax.swing.JButton();
        receiveTeamButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        visitedHallButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        bestNotesButton = new javax.swing.JButton();
        sqlField = new javax.swing.JTextField();
        sqlFindButton = new javax.swing.JButton();
        teamComboBox = new javax.swing.JComboBox();

        mainPanel.setName("mainPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 677, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(warehouse.WarehouseApp.class).getContext().getResourceMap(WarehouseView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(warehouse.WarehouseApp.class).getContext().getActionMap(WarehouseView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        recordTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        recordTable.setName("recordTable"); // NOI18N
        jScrollPane1.setViewportView(recordTable);
        recordTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("recordTable.columnModel.title0")); // NOI18N
        recordTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("recordTable.columnModel.title1")); // NOI18N
        recordTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("recordTable.columnModel.title2")); // NOI18N
        recordTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("recordTable.columnModel.title3")); // NOI18N

        mostPointingButton.setText(resourceMap.getString("mostPointingButton.text")); // NOI18N
        mostPointingButton.setName("mostPointingButton"); // NOI18N
        mostPointingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mostPointingButtonActionPerformed(evt);
            }
        });

        receiversPlayersButton.setText(resourceMap.getString("receiversPlayersButton.text")); // NOI18N
        receiversPlayersButton.setName("receiversPlayersButton"); // NOI18N
        receiversPlayersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                receiversPlayersButtonActionPerformed(evt);
            }
        });

        attackersPlayersButton.setText(resourceMap.getString("attackersPlayersButton.text")); // NOI18N
        attackersPlayersButton.setName("attackersPlayersButton"); // NOI18N
        attackersPlayersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attackersPlayersButtonActionPerformed(evt);
            }
        });

        defendersPlayersButton.setText(resourceMap.getString("defendersPlayersButton.text")); // NOI18N
        defendersPlayersButton.setName("defendersPlayersButton"); // NOI18N
        defendersPlayersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defendersPlayersButtonActionPerformed(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        attackTeamButton.setText(resourceMap.getString("attackTeamButton.text")); // NOI18N
        attackTeamButton.setName("attackTeamButton"); // NOI18N
        attackTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                attackTeamButtonActionPerformed(evt);
            }
        });

        defenceTeamButton.setText(resourceMap.getString("defenceTeamButton.text")); // NOI18N
        defenceTeamButton.setName("defenceTeamButton"); // NOI18N
        defenceTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defenceTeamButtonActionPerformed(evt);
            }
        });

        receiveTeamButton.setText(resourceMap.getString("receiveTeamButton.text")); // NOI18N
        receiveTeamButton.setName("receiveTeamButton"); // NOI18N
        receiveTeamButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                receiveTeamButtonActionPerformed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        visitedHallButton.setText(resourceMap.getString("visitedHallButton.text")); // NOI18N
        visitedHallButton.setName("visitedHallButton"); // NOI18N
        visitedHallButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visitedHallButtonActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        bestNotesButton.setText(resourceMap.getString("bestNotesButton.text")); // NOI18N
        bestNotesButton.setName("bestNotesButton"); // NOI18N
        bestNotesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bestNotesButtonActionPerformed(evt);
            }
        });

        sqlField.setText(resourceMap.getString("sqlField.text")); // NOI18N
        sqlField.setName("sqlField"); // NOI18N

        sqlFindButton.setText(resourceMap.getString("sqlFindButton.text")); // NOI18N
        sqlFindButton.setName("sqlFindButton"); // NOI18N
        sqlFindButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sqlFindButtonActionPerformed(evt);
            }
        });

        teamComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        teamComboBox.setName("teamComboBox"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 657, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(visitedHallButton)
                    .addComponent(jLabel4)
                    .addComponent(bestNotesButton)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(defendersPlayersButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(attackersPlayersButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(receiversPlayersButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(mostPointingButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(attackTeamButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(defenceTeamButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
                            .addComponent(receiveTeamButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                        .addGap(40, 40, 40))
                    .addComponent(jLabel2)
                    .addComponent(teamComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                        .addComponent(sqlField)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sqlFindButton)))
                .addContainerGap())
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(statusMessageLabel)
                                .addComponent(statusAnimationLabel))
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addComponent(mostPointingButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(receiversPlayersButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(attackersPlayersButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(defendersPlayersButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(teamComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(attackTeamButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(defenceTeamButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(receiveTeamButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(visitedHallButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE))
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bestNotesButton))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(sqlFindButton)
                            .addComponent(sqlField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void mostPointingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mostPointingButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String firstName = "";
        String teamName = "";
        sorted_map_int = db.getTheBestPointPlayers();
        for (int key : sorted_map_int.keySet()) {
            name = db.getPlayersNameById(key);
            firstName = db.getPlayersFirstNameById(key);
            teamName = db.getPlayersTeamNameById(key);
            recordTable.setValueAt(firstName,i,0);
            recordTable.setValueAt(name,i,1);
            recordTable.setValueAt(teamName,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map_int.get(key)),i,3);
            //System.out.println(i + ") " + firstName + " " + name + " " + teamName  + " / rate: " + twoDForm.format(sorted_map.get(key)));
            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_mostPointingButtonActionPerformed

    private void receiversPlayersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_receiversPlayersButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String firstName = "";
        String teamName = "";
        sorted_map = db.getBestPlayersGambitsRate();
        for (int key : sorted_map.keySet()) {
            name = db.getPlayersNameById(key);
            firstName = db.getPlayersFirstNameById(key);
            teamName = db.getPlayersTeamNameById(key);
            recordTable.setValueAt(firstName,i,0);
            recordTable.setValueAt(name,i,1);
            recordTable.setValueAt(teamName,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            //System.out.println(i + ") " + firstName + " " + name + " " + teamName  + " / rate: " + twoDForm.format(sorted_map.get(key)));
            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_receiversPlayersButtonActionPerformed

    private void attackersPlayersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attackersPlayersButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String firstName = "";
        String teamName = "";
        //System.out.println("Najlepsza hala: " + db.getTheBestHallSpectatorsRate());
        sorted_map = db.getBestPlayersAttackRate();
        for (int key : sorted_map.keySet()) {
            name = db.getPlayersNameById(key);
            firstName = db.getPlayersFirstNameById(key);
            teamName = db.getPlayersTeamNameById(key);
            //System.out.println(i + ") " + firstName + " " + name + " " + teamName  + " / rate: " + twoDForm.format(sorted_map.get(key)));
            recordTable.setValueAt(firstName,i,0);
            recordTable.setValueAt(name,i,1);
            recordTable.setValueAt(teamName,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_attackersPlayersButtonActionPerformed

    private void defendersPlayersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defendersPlayersButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String firstName = "";
        String teamName = "";
        sorted_map = db.getBestPlayersBlocksRate();
        for (int key : sorted_map.keySet()) {
            name = db.getPlayersNameById(key);
            firstName = db.getPlayersFirstNameById(key);
            teamName = db.getPlayersTeamNameById(key);
            recordTable.setValueAt(firstName,i,0);
            recordTable.setValueAt(name,i,1);
            recordTable.setValueAt(teamName,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            //System.out.println(i + ") " + firstName + " " + name + " " + teamName  + " / rate: " + twoDForm.format(sorted_map.get(key)));
            i++;
            if (i >= tableSize){
                break;
            }
        }
    }//GEN-LAST:event_defendersPlayersButtonActionPerformed

    private void attackTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attackTeamButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String country = "";
        String points = "";
        sorted_map = db.getBestTeamsAttackRate();
        for (int key : sorted_map.keySet()) {
            name = db.getTeamsNameById(key);
            country = db.getTeamsCountryById(key);
            points = Integer.toString(db.getTeamsPointsById(key));
            recordTable.setValueAt(name,i,0);
            recordTable.setValueAt(country,i,1);
            recordTable.setValueAt(points,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            //System.out.println(i + ") " + firstName + " " + name + " " + teamName  + " / rate: " + twoDForm.format(sorted_map.get(key)));
            i++;
            if (i >= tableSize){
                break;
            }
        }
    }//GEN-LAST:event_attackTeamButtonActionPerformed

    private void defenceTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defenceTeamButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String country = "";
        String points = "";
        sorted_map = db.getBestTeamsBlocksRate();
        for (int key : sorted_map.keySet()) {
            name = db.getTeamsNameById(key);
            country = db.getTeamsCountryById(key);
            points = Integer.toString(db.getTeamsPointsById(key));
            recordTable.setValueAt(name,i,0);
            recordTable.setValueAt(country,i,1);
            recordTable.setValueAt(points,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            i++;
            if (i >= tableSize){
                break;
            }
        }
    }//GEN-LAST:event_defenceTeamButtonActionPerformed

    private void receiveTeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_receiveTeamButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String country = "";
        String points = "";
        sorted_map = db.getBestTeamsGambitsRate();
        for (int key : sorted_map.keySet()) {
            name = db.getTeamsNameById(key);
            country = db.getTeamsCountryById(key);
            points = Integer.toString(db.getTeamsPointsById(key));
            recordTable.setValueAt(name,i,0);
            recordTable.setValueAt(country,i,1);
            recordTable.setValueAt(points,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_receiveTeamButtonActionPerformed

    private void visitedHallButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visitedHallButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String city = "";
        String capacity = "";
        sorted_map = db.getBestHallSpectatorsRate();
        for (int key : sorted_map.keySet()) {
            name = db.getHallNameById(key);
            city = db.getHallCityById(key);
            capacity = Integer.toString(db.getHallCapacityById(key));
            recordTable.setValueAt(name,i,0);
            recordTable.setValueAt(city,i,1);
            recordTable.setValueAt(capacity,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_visitedHallButtonActionPerformed

    private void bestNotesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bestNotesButtonActionPerformed
        createTable();
        int i = 0;
        String name = "";
        String firstName = "";
        String nationality = "";
        sorted_map = db.getBestArbitersRate();
        for (int key : sorted_map.keySet()) {
            name = db.getArbitersNameById(key);
            firstName = db.getArbitersFirstNameById(key);
            nationality = db.getArbitersNationalityById(key);
            recordTable.setValueAt(firstName,i,0);
            recordTable.setValueAt(name,i,1);
            recordTable.setValueAt(nationality,i,2);
            recordTable.setValueAt(twoDForm.format(sorted_map.get(key)),i,3);
            //System.out.println(i + ") " + firstName + " " + name + " " + nationality  + " / rate: " + twoDForm.format(sorted_map.get(key)));            i++;
            if (i >= tableSize) {
                break;
            }
        }
    }//GEN-LAST:event_bestNotesButtonActionPerformed

    private void sqlFindButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sqlFindButtonActionPerformed
        createTable();
        Vector<String> vector;
        vector = db.executeQuery(sqlField.getText());
        int c = Integer.parseInt(vector.get(vector.size()-1));
        int col=0, row=0;
        
        for (int i=0; i<vector.size()-1; i++) {
            if ( i/c >= tableSize)
                break;
            System.out.println(i%c+"("+i+"): "+vector.get(i));
            recordTable.setValueAt(vector.get(i),i/c,i%c);
;
        }
    }//GEN-LAST:event_sqlFindButtonActionPerformed

    public void createTable(){
        DefaultTableModel model = (DefaultTableModel)recordTable.getModel();
        for(int i=recordTable.getRowCount()-1;i>=0;i--){
            model.removeRow(i);
        }
        for(int i=0;i<tableSize;i++){
            model.addRow((Vector)null);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton attackTeamButton;
    private javax.swing.JButton attackersPlayersButton;
    private javax.swing.JButton bestNotesButton;
    private javax.swing.JButton defenceTeamButton;
    private javax.swing.JButton defendersPlayersButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JButton mostPointingButton;
    private javax.swing.JButton receiveTeamButton;
    private javax.swing.JButton receiversPlayersButton;
    private javax.swing.JTable recordTable;
    private javax.swing.JTextField sqlField;
    private javax.swing.JButton sqlFindButton;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JComboBox teamComboBox;
    private javax.swing.JButton visitedHallButton;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
